home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / hexedit107 folder.sit / hexedit107 folder / HexEditSource / Source / Main.c < prev    next >
C/C++ Source or Header  |  1994-09-25  |  10KB  |  419 lines

  1. /************************************************************************************
  2.  * Main.c
  3.  *
  4.  * from HexEdit, a simple hex editor
  5.  * copyright 1993, Jim Bumgardner
  6.  *
  7.  * Revision History is in History.note
  8.  ************************************************************************************/
  9. #include "HexEdit.h"
  10. #include "AppleEvents.h"
  11. #include <Traps.h>
  12.  
  13.  
  14. void     MyInitMacintosh(void);
  15. void    MyInitMultifinder(void);
  16. void     InitAppleEvents(void);
  17. void    CheckEnvironment(void);
  18. void     MyHandleEvent(void);
  19. void     MyDoEvent(EventRecord *theEvent);
  20. void    IdleObjects(EventRecord *er);
  21.  
  22. Boolean    gWNEImplemented,gQuitFlag,gSys7Flag,gColorQDFlag;
  23. // Typical Macintosh Initialization Code
  24.  
  25.  
  26. // Main Entry Point
  27.  
  28. main()
  29. {
  30.     // Standard Mac Initialization
  31.     MyInitMacintosh();
  32.  
  33.     // Check if Multifinder (WaitNextEvent) is implemented
  34.     MyInitMultifinder();
  35.  
  36.     // Check if System 7
  37.     CheckEnvironment();
  38.  
  39.     // Init Apple Events
  40.     InitAppleEvents();
  41.  
  42.     // Set up the menu bar
  43.     MySetUpMenus();
  44.  
  45.     InitializeEditor();
  46.  
  47.     if (!gSys7Flag)
  48.         AskEditWindow();
  49.  
  50.     // main event loop
  51.     while (!gQuitFlag)            // Till the End of Time...
  52.         MyHandleEvent();        // Get an Event, do something about it
  53.  
  54.     CleanupEditor();
  55. }
  56.  
  57. // Standard Macintosh Initialization
  58.  
  59. void MyInitMacintosh(void)
  60. {
  61.     MaxApplZone();
  62.     
  63.     InitGraf(&thePort);
  64.     InitFonts();
  65.     FlushEvents(everyEvent, 0);
  66.     InitWindows();
  67.     InitMenus();
  68.     TEInit();
  69.     InitDialogs(0L);
  70.     InitCursor();
  71. }
  72.  
  73. // Check if WaitNextEvent (Multifinder) is implemented on this Macintosh
  74.  
  75. void MyInitMultifinder(void)
  76. {
  77.     gWNEImplemented = (NGetTrapAddress(_WaitNextEvent, ToolTrap) != 
  78.                        NGetTrapAddress(_Unimplemented,ToolTrap));
  79. }
  80.  
  81. // The Main Event Dispatcher - this routine should be called repeatedly
  82.  
  83. void MyHandleEvent(void)
  84.  
  85. {
  86.     EventRecord    theEvent;
  87.     Boolean        ok;
  88.  
  89.     // If the more modern WaitNextEvent is implemented, use it 
  90.  
  91.     if (gWNEImplemented)
  92.         // We don't have to call SystemTask because WaitNextEvent calls it for us
  93.         // Get the next event
  94.         ok = WaitNextEvent(everyEvent,&theEvent,0L,NULL);
  95.  
  96.     else {
  97.         // we are running in (Single) Finder under system 6 or less
  98.         // Give Desk Accessories some processing time
  99.         SystemTask ();
  100.  
  101.         // Get the next event
  102.         ok = GetNextEvent (everyEvent, &theEvent);
  103.     }
  104.  
  105.     if (IsDialogEvent(&theEvent)) {
  106.         DoModelessDialogEvent(&theEvent);
  107.     }
  108.     else if (ok) {
  109.             // Handle the Event
  110.             MyDoEvent(&theEvent);
  111.     }
  112.     else {
  113.         // Nothing happened, kick back...
  114.         IdleObjects(&theEvent);
  115.     }
  116. }
  117.  
  118.  
  119. void MyDoEvent(EventRecord *theEvent)
  120. {
  121.     short         windowCode;
  122.     WindowPtr    theWindow;
  123.  
  124.  
  125.     switch (theEvent->what) {
  126.     //
  127.     // Was the mouse button pressed?
  128.     case mouseDown:
  129.         // Find out where the mouse went down
  130.         windowCode = FindWindow (theEvent->where, &theWindow);
  131.  
  132.           switch (windowCode) {
  133.         case inSysWindow:     // Desk Accessory?
  134.             SystemClick (theEvent, theWindow);
  135.             break;
  136.             
  137.         case inMenuBar:        // Menu Bar?
  138.               MyAdjustMenus();
  139.             MyHandleMenu(MenuSelect(theEvent->where));
  140.             break;
  141.  
  142.         default:            // Cursor was inside our window
  143.             // If the window isn't in the front
  144.             if (theWindow != FrontWindow()) {
  145.                 // Make it so...
  146.                 SelectWindow(theWindow);
  147.                 MyAdjustMenus();
  148.             }
  149.             else {
  150.                 // Window is already in the front, handle the click
  151.                 switch (windowCode) {
  152.  
  153.                 case inContent:        // Content area?
  154.                     if (((WindowPeek) theWindow)->refCon == MyWindowID)
  155.                         ((ObjectWindowPtr) theWindow)->HandleClick(theWindow, theEvent->where, theEvent);
  156.                     break;
  157.  
  158.                 case inDrag:        // Dragbar?
  159.                     {
  160.                         Rect    dragRect;
  161.                         dragRect = screenBits.bounds;
  162.                         // Handle the dragging of the window
  163.                         DragWindow(theWindow, theEvent->where, &dragRect);
  164.                         if (!((ObjectWindowPtr) theWindow)->floating)
  165.                             SelectWindow(theWindow);
  166.                     }
  167.                     break;
  168.  
  169.                  case inGoAway:                        // close box?
  170.                       if (TrackGoAway(theWindow, theEvent->where)) {
  171.                         // If mouse is released inside the close box
  172.                         // Hide or close the window
  173.                           if (((WindowPeek) theWindow)->refCon == MyWindowID)
  174.                             CloseEditWindow(theWindow);
  175.                         else if (theWindow == gSearchWin) {
  176.                             DisposDialog(gSearchWin);
  177.                             gSearchWin = NULL;
  178.                         }
  179.                         MyAdjustMenus();
  180.                     }
  181.                       break;
  182.  
  183.                 case inGrow:                        // Grow box?
  184.                     {
  185.                         long    growResult;
  186.                         Rect    growRect;
  187.  
  188.                         SelectWindow(theWindow);
  189.  
  190.                         SetRect(&growRect,MaxWindowWidth+SBarSize-1,64,
  191.                                 MaxWindowWidth+SBarSize-1,gMaxHeight);
  192.  
  193.                         // Handle the mouse tracking for the resizing
  194.                         growResult = GrowWindow(theWindow,theEvent->where,&growRect);
  195.  
  196.                         // Change the size of the window
  197.                         SizeWindow(theWindow,LoWord(growResult),HiWord(growResult),true);
  198.  
  199.                         AdjustScrollBars(theWindow, true);
  200.                         DrawPage((EditWindowPtr) theWindow);
  201.  
  202.                         // Redraw the window
  203.                         SetPort(theWindow);
  204.                           InvalRect(&theWindow->portRect);
  205.                     }
  206.                     break;
  207.                 case inZoomIn:
  208.                 case inZoomOut:
  209.                     if (TrackBox(theWindow, theEvent->where, windowCode)) {
  210.                         SetPort(theWindow);
  211.                         EraseRect(&theWindow->portRect);
  212.                         ZoomWindow(theWindow, windowCode, true);
  213.                         AdjustScrollBars(theWindow, true);
  214.                         DrawPage((EditWindowPtr) theWindow);
  215.                         // Redraw the window
  216.                         SetPort(theWindow);
  217.                           InvalRect(&theWindow->portRect);
  218.                     }
  219.                 }
  220.             }
  221.             break;
  222.         }
  223.         break;
  224.         
  225.     // Was a key pressed?
  226.     case keyDown: 
  227.     case autoKey:
  228.         // Was the cmd-key being held down?  If so, process menu bar short cuts.
  229.         if ((theEvent->modifiers & cmdKey) != 0) {
  230.           MyAdjustMenus();
  231.           MyHandleMenu(MenuKey((char) (theEvent->message & charCodeMask)));
  232.         }
  233.         else {
  234.             theWindow = FrontWindow();
  235.             if (((WindowPeek) theWindow)->refCon == MyWindowID &&
  236.                 ((ObjectWindowPtr) theWindow)->ProcessKey != NULL)
  237.                 ((ObjectWindowPtr) theWindow)->ProcessKey(theWindow, theEvent);
  238.         }
  239.         break;
  240.  
  241.     // Does a window need to be redrawn?
  242.     case updateEvt:
  243.         theWindow = (WindowPtr) theEvent->message;
  244.         if (((WindowPeek) theWindow)->refCon == MyWindowID)
  245.             ((ObjectWindowPtr) theWindow)->Update(theWindow);
  246.         break;
  247.  
  248.     // Has a window been activated or deactivated?
  249.     case activateEvt:
  250.         theWindow = (WindowPtr) theEvent->message;
  251.  
  252.         // Force it to be redrawn
  253.         if (((WindowPeek) theWindow)->refCon == MyWindowID)
  254.             ((ObjectWindowPtr) theWindow)->Activate(theWindow,(theEvent->modifiers & activeFlag) > 0);
  255.  
  256.         break;
  257.     case osEvt:
  258.         // Force it to be redrawn
  259.         switch (theEvent->message >> 24) {
  260.         case suspendResumeMessage:
  261.             theWindow = FrontWindow();
  262.             if (theWindow && ((WindowPeek) theWindow)->refCon == MyWindowID)
  263.                 ((ObjectWindowPtr) theWindow)->Activate(theWindow,(theEvent->message & resumeFlag) > 0);
  264.             break;
  265.         }
  266.         break;        
  267.     case kHighLevelEvent:
  268.         if (gSys7Flag)
  269.             AEProcessAppleEvent(theEvent);
  270.         break;
  271.     }
  272. }        
  273.  
  274. // Do Idle Time Processing
  275.  
  276. void IdleObjects(EventRecord *er)
  277. {
  278.     WindowPeek    theWin;
  279.     theWin = (WindowPeek) FrontWindow();
  280.     while (theWin) {
  281.         if (theWin->refCon == MyWindowID &&
  282.             ((ObjectWindowPtr) theWin)->Idle)
  283.             ((ObjectWindowPtr) theWin)->Idle((WindowPtr) theWin, er);
  284.         theWin = theWin->nextWindow;
  285.     }
  286. }
  287.  
  288.  
  289.  
  290. Boolean GotRequiredParams(AppleEvent *theEvent)
  291. {
  292.    DescType returnedType;
  293.    Size     actualSize;
  294.    OSErr    err;
  295.    err = AEGetAttributePtr ( theEvent, keyMissedKeywordAttr, 
  296.                         typeWildCard, &returnedType, NULL, 0, 
  297.                         &actualSize);
  298.    
  299.    return err == errAEDescNotFound;
  300.    
  301.  }    /* CAppleEvent::GotRequiredParams */
  302.  
  303.  
  304. void DoOpenEvent(AppleEvent *theEvent)
  305. {
  306.     Handle        docList = NULL;
  307.     long        i, numDocs;
  308.     FSSpec        myFSS;
  309.     DescType    eventID;
  310.     AEDescList    theList;
  311.     AEKeyword    aeKeyword=keyDirectObject;
  312.     long        itemCount;
  313.     DescType    actualType;
  314.     Size        actualSize;
  315.     OSErr        oe;
  316.  
  317.     if ((oe = AEGetParamDesc( theEvent, keyDirectObject, typeAEList, &theList)) != noErr) {
  318.         DebugStr("¥pAEGetParamDesc");
  319.         return;
  320.     }
  321.  
  322.  
  323.     if (!GotRequiredParams(theEvent)) {
  324.         DebugStr("¥pGotRequiredParams");
  325.         return;
  326.     }
  327.  
  328.     if ((oe = AECountItems( &theList, &itemCount)) != noErr) {
  329.         DebugStr("¥pAECountItems");
  330.         return;
  331.     }
  332.  
  333.  
  334.     for (i = 1; i <= itemCount; i++)
  335.     {
  336.         oe = AEGetNthPtr( &theList, i, typeFSS, &aeKeyword, &actualType,
  337.                         (Ptr) &myFSS, sizeof( FSSpec), &actualSize);
  338.  
  339.         if (oe == noErr) {
  340.             OpenEditWindow(&myFSS);
  341.         }
  342.     }
  343.     AEDisposeDesc(&theList);
  344.     // event was handled successfully
  345. }
  346.  
  347. pascal OSErr AppleEventHandler(AppleEvent *theEvent,AppleEvent *reply, long refCon)
  348. {    
  349.     OSErr        err;
  350.     DescType    actualType;
  351.     Size        actualSize;
  352.     DescType    eventClass, eventID;
  353.     OSErr        oe;
  354.  
  355.     if ((oe = AEGetAttributePtr( (AppleEvent*) theEvent, keyEventClassAttr,
  356.                     typeType, &actualType, (Ptr) &eventClass, 
  357.                     sizeof(eventClass), &actualSize)) != noErr)
  358.             return oe;
  359.                             
  360.     
  361.     if ((oe = AEGetAttributePtr(  (AppleEvent*) theEvent, keyEventIDAttr,
  362.                     typeType, &actualType, (Ptr) &eventID, 
  363.                     sizeof(eventID), &actualSize)) != noErr)
  364.             return oe;
  365.                                     
  366.     if (eventClass == kCoreEventClass)
  367.     {
  368.         switch (eventID)
  369.         {
  370.         case kAEOpenApplication:
  371.             if (GotRequiredParams(theEvent))
  372.             {
  373.                 // gGopher->DoCommand( cmdNew);
  374.                 // anAppleEvent->SetErrorResult( noErr);
  375.                 AskEditWindow();
  376.             }
  377.             break;
  378.                 
  379.         case kAEOpenDocuments:
  380.             DoOpenEvent( theEvent);
  381.             break;
  382.                 
  383.         case kAEPrintDocuments:
  384.             break;
  385.             
  386.         case kAEQuitApplication:
  387.             if (GotRequiredParams(theEvent))
  388.             {
  389.                 gQuitFlag = true;
  390.             }
  391.             break;
  392.         }        
  393.     }
  394.         
  395.     return noErr;
  396. }
  397.  
  398.  
  399.  
  400. void InitAppleEvents(void)
  401. {
  402.     if (gSys7Flag)
  403.         AEInstallEventHandler(typeWildCard, typeWildCard,
  404.                                 (EventHandlerProcPtr) AppleEventHandler,
  405.                                 0,FALSE);
  406. }
  407.  
  408. void CheckEnvironment(void)
  409. {
  410.     SysEnvRec    sEnv;
  411.     OSErr        oe;
  412.  
  413.     oe = SysEnvirons(1,&sEnv);
  414.  
  415.     gSys7Flag = sEnv.systemVersion >= 0x0700;
  416.     gColorQDFlag = sEnv.hasColorQD;
  417. }
  418.  
  419. /* end Evtlab.c */